//
//  ScrollViewPage2.swift
//  CHSimpleUI
//
//  Created by cyptt on 2023/5/6.
//

/**
 自动定tablewview 切换，可以滑动
 */
import UIKit
public class ScrollViewPage2: UIView, UIScrollViewDelegate {

    private let SCREEN_W:CGFloat = UIScreen.main.bounds.width
    private let SCREEN_H:CGFloat = UIScreen.main.bounds.height
    

    private var views:[UIView] = []
    private var selfW:CGFloat = UIScreen.main.bounds.width
    private var selfH:CGFloat = UIScreen.main.bounds.height
    
    private let top_h:CGFloat = 44
    private var isFirstLoad:Bool = false
   
    private var topBtnViewW:CGFloat = UIScreen.main.bounds.width
    
    private var direction:CHSimpleDirection = .center
    private var marign:CGFloat = 16
    private var clickItemCallBack:((_ index:Int,_ title:String?) -> ())?
    
    public override init(frame: CGRect) {
        super.init(frame: frame)
        self.setUI()
        self.setPosition()
        self.isFirstLoad = true
    }
    /**
        @param topBtnViewW 宽度 默认屏幕宽度
     */
    public init(frame: CGRect,topBtnViewW:CGFloat = UIScreen.main.bounds.width,direction:CHSimpleDirection = .center,marign:CGFloat = 16){
        super.init(frame: frame)
        self.isFirstLoad = true
        self.topBtnViewW = topBtnViewW
        self.direction = direction
        self.marign = marign
        self.setUI()
        self.setPosition()
      
       
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    public func setViews(views:[UIView]){
      
        self.views = views
        self.initSigleView()
    }
    
    public func btnClickIndexBlockFun(clouse:@escaping(_ index:Int,_ title:String?)-> Void){
        self.clickItemCallBack = clouse
    }
 
    func setUI(){
        self.addSubview(self.titleL)
        self.addSubview(self.singleChioseView)
        self.addSubview(self.lineView)
        self.addSubview(self.scrollView)

    }
    
  
    public override func layoutSubviews() {
        super.layoutSubviews()
        
       
        let selfW:CGFloat = self.frame.size.width
        let selfH:CGFloat = self.frame.size.height
        
        if self.isFirstLoad == true{
            print("selfH==",selfH)
            self.selfW = selfW
            self.selfH = selfH
            let scrollViewH = self.scrollView.frame.size.height
            print("scrollViewH==",scrollViewH)
            self.scrollView.contentSize = CGSize.init(width: CGFloat(self.views.count) * selfW, height: scrollViewH)
            
            var i = 0
            for view in self.views{
                self.scrollView.addSubview(view)
                view.snp.makeConstraints { make in
                    make.left.equalTo(self.selfW * CGFloat(i))
                    make.width.equalToSuperview()
                    make.top.equalTo(self.singleChioseView.snp.bottom)
                    make.height.equalTo(scrollViewH)
                }
                
                i = i + 1
            }
        }
       
        self.isFirstLoad = false
        
    }
    
    func initSigleView(){
        

        weak var weakSelf = self
        self.singleChioseView.btnClickIndexBlockFun { index, title in
      
            let point:CGPoint = CGPoint.init(x: Int(self.selfW)*index, y: 0)
            weakSelf?.scrollView.setContentOffset(point, animated: true)
          
            if weakSelf?.clickItemCallBack != nil{
                weakSelf!.clickItemCallBack!(index,title)
            }
        }
        

    }
    
    func setPosition(){
        
        if self.direction == .right{
            self.singleChioseView.snp.makeConstraints { make in
        
                make.right.equalTo(-self.marign)
                make.top.equalTo(10)
                make.width.equalTo(self.topBtnViewW)
                make.height.equalTo(top_h)
            }
        }else if self.direction == .left{
            self.singleChioseView.snp.makeConstraints { make in
        
                make.left.equalTo(self.marign)
                make.top.equalTo(10)
                make.width.equalTo(self.topBtnViewW)
                make.height.equalTo(top_h)
            }
        }else if self.direction == .center{
            self.singleChioseView.snp.makeConstraints { make in
                make.centerX.equalToSuperview()
                make.top.equalTo(10)
                make.width.equalTo(self.topBtnViewW)
                make.height.equalTo(top_h)
            }
        }
      
        
        self.titleL.snp.makeConstraints { make in
            make.left.equalTo(self.marign)
            make.centerY.equalTo(self.singleChioseView)
        }
        self.scrollView.snp.makeConstraints { make in
            make.left.equalTo(0)
            make.right.equalTo(0)
            make.top.equalTo(self.lineView.snp.bottom)
            make.bottom.equalToSuperview()
//            if #available(iOS 11.0, *) {
//                make.bottom.equalTo(self.safeAreaLayoutGuide)
//            } else {
//                make.bottom.equalTo(0)
//            }
        }
        
        self.lineView.snp.makeConstraints { make in
            make.left.equalTo(0)
            make.right.equalTo(0)
            make.top.equalTo(self.singleChioseView.snp.bottom)
            make.height.equalTo(0.5)
        }
    }
    
    
    //MARK: ---lazy
    public lazy var singleChioseView: CHSingleChioseScroll = {
        
        let v : CHSingleChioseScroll = CHSingleChioseScroll()
        return v
    }()
    
    public lazy var lineView: UIView = {
        
        let v : UIView = UIView()
        v.backgroundColor = UIColor.init(red: 240/255.0, green: 240/255.0, blue: 240/255.0, alpha: 240/255.0)
        return v
    }()
    
    lazy var titleL: UILabel = {
        
        let v : UILabel = UILabel()
        v.isHidden = true
        v.text = "请输入"
        v.textColor = UIColor.init(red: 51/255.0, green: 51/255.0, blue: 51/255.0, alpha: 1)
        v.font = UIFont.systemFont(ofSize: 16)
        return v
    }()
    
    //开放
   public lazy var scrollView: UIScrollView = {
        let v:UIScrollView = UIScrollView()
        v.backgroundColor = UIColor.clear
        v.delegate = self;
        v.isPagingEnabled = true; //整夜翻
        v.showsVerticalScrollIndicator = false;
        v.showsHorizontalScrollIndicator = false;
      

        return v
    }()
    
    
  
    
}


//MARK: -滚动
extension ScrollViewPage2{
   
    

 
  
//    //滚动视图减速完成，滚动将停止时，调用该方法。一次有效滑动，只执行一次
//    - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView;{
//        NSLog(@"%sx=%fy=%f",__func__,scrollView.contentOffset.x,scrollView.contentOffset.y);
//
//        int index = 0;
//
//        index = (scrollView.contentOffset.x/SCREEN_WIDTH);
//
//        [self.singleView setIndex:index];
//    }


  

    public func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        var index = 0
        
        index = Int(scrollView.contentOffset.x)/Int(self.selfW)
        self.singleChioseView.selectIndex(index: index)
     
    }
    

    public func scrollViewShouldScrollToTop(_ scrollView: UIScrollView) -> Bool {
        
        return true
    }
  
//    //指示当用户点击状态栏后，滚动视图是否能够滚动到顶部。需要设置滚动视图的属性：
//    - (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView; {
//
//        return YES;
//    }

 
}



extension ScrollViewPage2{
    
    //解决右滑手势与scrollview 冲突问题
    public override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
        //意思是，触摸X点小于10， 我scrollView不要这个手势，
        if point.x < 10 {
            return nil
        }//让它把这个手势给别人。
        return super.hitTest(point, with: event)
    }
}

extension ScrollViewPage2{
    public func showTitle(title:String?){
        self.titleL.isHidden = false
        self.titleL.text = title
    }
}




